package com.ycxy.springbootmybatis.service.impl;

import com.ycxy.springbootmybatis.entity.User;
import com.ycxy.springbootmybatis.mapper.UserMapper;
import com.ycxy.springbootmybatis.service.UserService;
import org.apache.ibatis.annotations.CacheNamespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@CacheConfig(cacheNames = "user")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Cacheable(key = "#id")
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    @CachePut(key = "#id")
    public User UpdateUser(int id,String username) {
        User userById = getUserById(id);
        userById.setUsername(username);
        userMapper.updateUser(userById);
        return getUserById(id);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

}
